/***************************************************************************
 *   Copyright (C) 2008 by Ivan Blagoev Topolsky   *
 *   ivan.topolsky@genebio.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "utils/aminoacidinstancerbasic.h"
#include "utils/peptidemodifier.h"
#include "utils/enzymetrypsin.h"
#include "utils/dbsequencefactoryfasta.h"
#include "utils/dbsequencedigestorfactory.h"

#include <iostream>
#include <unistd.h>

#define STRINGIFY(x) XSTRINGIFY(x)
#define XSTRINGIFY(x) #x

using namespace std;
using namespace bol;

int doit(const char *fname) {
  int nbentries = 0;
  DBSequenceFactoryFasta fasta;

  AminoAcidInstancerBasic aainstancer;
  aainstancer.setMasses();

  //build a list of peptide out of the passed one and the modif set in the mofifier
  PeptideModifier peptmodifier;
  peptmodifier.addModif(new Modification("Cys_CAM C 57"), false);
  peptmodifier.addModif(new Modification("Oxidation HMW 16"), true);

  fasta.openFile(fname);


  Enzyme *enzyme=new EnzymeTrypsin();
  vector<Peptide> vcleavedpept;

  DBSequenceDigestorFactory peptides(&fasta, enzyme, 1);
  PeptideFromSequencePtr ptr;
  DBSequencePtr oldseq;

  cout << "starting..." << endl;
  while(peptides.nextPeptide(ptr)){
      // keep track if we moved to a new entry in the database so we can write a nice header
      if (oldseq != ptr->DBSequence()) { 
        oldseq = ptr->DBSequence(); // this will also release oldseq since it was the last reference.
        cout	<< endl
        	<< oldseq->ac() << endl
        	<< "\t" << oldseq->description() << endl;
      }

      // write each peptide independently of where it did come from
      cout	<< ptr->DBSequence()->ac()<<"\t"
		<< ptr->position()<<","
		<< ptr->length()<<"\t";
      peptmodifier.printPeptide(cout, &(*ptr));
      cout<<endl;
  }
  return 0;
}

int main(int argc, char **argv){
  return doit(argc>1 ? argv[1] : STRINGIFY(DATADIR) "/little.fasta");
}
